﻿using System.Data.Services;
using System.Threading;

namespace AstoriaPlus
{
	public static class AuthenticationQueryExtensions
	{
		public static QueryExpression<TEntity, DataServiceQueryContext<TDataService, TDataSource>> IsAuthenticated<TDataService, TDataSource, TEntity>(this IEntityQueryRules<TDataService, TDataSource, TEntity> entityRules)
			where TDataService : DataService<TDataSource>
			where TDataSource : class
			where TEntity : class
		{
			return entityRules.Where(x => IsAuthenticated());
		}

		private static bool IsAuthenticated()
		{
			var identity = Thread.CurrentPrincipal != null ? Thread.CurrentPrincipal.Identity : null;
			return identity != null && identity.IsAuthenticated;
		}
	}
}
